LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITY lcd_show2 IS
PORT(clk:IN STD_LOGIC;
state_in:IN INTEGER RANGE 0 TO 4;
sech_decode,secl_decode:IN STD_LOGIC_VECTOR(7 DOWNTO 0);
rs,rw,en:OUT STD_LOGIC;
data:OUT STD_LOGIC_VECTOR(7 DOWNTO 0);
--q:IN STD_LOGIC_VECTOR(7 DOWNTO 0);
--rdaddress:BUFFER STD_LOGIC_VECTOR(4 DOWNTO 0);
ram_in0,ram_in1,ram_in2,ram_in3,ram_in4,ram_in5,ram_in6,ram_in7,ram_in8:IN STD_LOGIC_VECTOR(7 DOWNTO 0);
record_switch:IN STD_LOGIC;
--next_record:IN STD_LOGIC
record_1,record_2,record_3:IN STD_LOGIC
);
END lcd_show2;

ARCHITECTURE fwm OF lcd_show2 IS
TYPE states IS (set_function,set_function2,clear_display,set_cursor,set_display,set_ddram1,write_data1,set_ddram2,write_data2);
SIGNAL state:states;
SIGNAL init:STD_LOGIC;


TYPE ram IS ARRAY (0 TO 15) OF STD_LOGIC_VECTOR(7 DOWNTO 0);
--CONSTANT raml:ram:=(X"20",X"57",X"65",X"6C",X"63",X"6F",X"6D",X"65",X"20",X"54",X"6F",X"20",X"5A",X"4A",X"55",X"20");
--CONSTANT ram2:ram:=(X"20",X"45",X"26",X"45",X"20",X"4C",X"61",X"62",X"20",X"43",X"65",X"6E",X"74",X"65",X"72",X"20");
SIGNAL lcd_clk:STD_LOGIC;
SIGNAL ram1,ram2:ram;
SIGNAL sech_temp,secl_temp,state_temp:STD_LOGIC_VECTOR(7 DOWNTO 0);

BEGIN

rw<='0';en<=lcd_clk;

--ram1<=(X"20",X"20",X"20",X"20",X"20",X"20",X"20",sech_decode,secl_decode,X"20",X"20",X"20",X"20",X"20",X"20",X"20");

p1:PROCESS(record_switch,record_1,record_2,record_3,state_in)--define what will be display(ram1 and ram2)
VARIABLE cnt:INTEGER RANGE 0 TO 2;
--VARIABLE init:STD_LOGIC;
BEGIN 
--CASE state_in IS
	--WHEN 0=>ram2<=(X"20",X"20",X"20",X"20",X"57",X"65",X"6C",X"63",X"6F",X"6D",X"65",X"21",X"20",X"20",X"20",X"20");
	--WHEN 1=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"72",X"75",X"6E",X"69",X"6E",X"67",X"20",X"20",X"20",X"20",X"20");
	--WHEN 2=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"20",X"4C",X"65",X"66",X"74",X"20",X"20",X"20",X"20",X"20",X"20");
	--WHEN 3=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"72",X"69",X"67",X"68",X"74",X"20",X"20",X"20",X"20",X"20",X"20");
	--WHEN 4=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"20",X"53",X"74",X"6F",X"70",X"20",X"20",X"20",X"20",X"20",X"20");
--END CASE;
	CASE record_switch is
	WHEN '0'=>
		CASE state_in IS
		WHEN 0=>ram2<=(X"20",X"20",X"20",X"20",X"57",X"65",X"6C",X"63",X"6F",X"6D",X"65",X"21",X"20",X"20",X"20",X"20");
		WHEN 1=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"72",X"75",X"6E",X"69",X"6E",X"67",X"20",X"20",X"20",X"20",X"20");
		WHEN 2=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"20",X"4C",X"65",X"66",X"74",X"20",X"20",X"20",X"20",X"20",X"20");
		WHEN 3=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"72",X"69",X"67",X"68",X"74",X"20",X"20",X"20",X"20",X"20",X"20");
		WHEN 4=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"20",X"53",X"74",X"6F",X"70",X"20",X"20",X"20",X"20",X"20",X"20");
		END CASE;
		ram1<=(X"20",X"20",X"20",X"20",X"20",X"20",X"20",sech_decode,secl_decode,X"20",X"20",X"20",X"20",X"20",X"20",X"20");
	
	WHEN '1'=>
		--IF(init='0') THEN
		--rdaddress<="00000";init:='1';
		--END IF;
		--IF(next_record='1') THEN
			--cnt:=0;
		--ELSIF(lcd_clk'event and lcd_clk='1') THEN
			--IF(cnt=5) THEN
			--cnt:=5;
			--ELSE 
			--cnt:=cnt+1;
				--IF(cnt=0 OR cnt=2 OR cnt=4) THEN
					--IF(init='0') THEN
					--rdaddress<="00000";init:='1';
					--END IF;
				--rdaddress<=rdaddress+1;
				--END IF;
			--END IF;
		--END IF;
		
		--CASE cnt IS
			--WHEN 1=>sech_temp<=q;
			--WHEN 3=>secl_temp<=q;
			--WHEN 5=>state_temp<=q;
			--WHEN OTHERS=>null;
		--END CASE;
		
		--IF(next_record'event and next_record='1') THEN
		--IF(next_record='1') THEN
			--IF(cnt=2) THEN cnt:=0;
			--ELSE
			--cnt:=cnt+1;
			--end if;
		--END IF;
		
		IF(record_1='1' and record_2='0' and record_3='0') THEN
			cnt:=0;
		ELSIF(record_1='0' and record_2='1' and record_3='0') THEN
			cnt:=1;
		ELSIF(record_1='0' and record_2='0' and record_3='1') THEN
			cnt:=2;
		END IF;
		
		CASE cnt IS
			WHEN 0=>sech_temp<=ram_in0;secl_temp<=ram_in1;state_temp<=ram_in2;
			WHEN 1=>sech_temp<=ram_in3;secl_temp<=ram_in4;state_temp<=ram_in5;
			WHEN 2=>sech_temp<=ram_in6;secl_temp<=ram_in7;state_temp<=ram_in8;
		END CASE;
			
		
		ram1<=(X"20",X"20",X"20",X"20",X"20",X"20",X"20",sech_temp,secl_temp,X"20",X"20",X"20",X"20",X"20",X"20",X"20");
		
		CASE state_temp IS
			WHEN "00000000"=>ram2<=(X"20",X"20",X"20",X"20",X"57",X"65",X"6C",X"63",X"6F",X"6D",X"65",X"21",X"20",X"20",X"20",X"20");
			WHEN "00000001"=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"72",X"75",X"6E",X"69",X"6E",X"67",X"20",X"20",X"20",X"20",X"20");
			WHEN "00000010"=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"20",X"4C",X"65",X"66",X"74",X"20",X"20",X"20",X"20",X"20",X"20");
			WHEN "00000011"=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"72",X"69",X"67",X"68",X"74",X"20",X"20",X"20",X"20",X"20",X"20");
			WHEN "00000100"=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"20",X"53",X"74",X"6F",X"70",X"20",X"20",X"20",X"20",X"20",X"20");
			WHEN OTHERS=>ram2<=(X"20",X"20",X"20",X"20",X"20",X"45",X"72",X"72",X"6F",X"72",X"20",X"20",X"20",X"20",X"20",X"20");
		END CASE;
	END CASE;	
END PROCESS;

p2: PROCESS(clk)
CONSTANT m : INTEGER:=500000;	--50M分频到100Hz
VARIABLE cout: INTEGER RANGE 0 TO 50000000:=0;
BEGIN
IF clk'EVENT AND clk='0' THEN cout:=cout+1;
	IF cout<=m/2 THEN lcd_clk<='1';
	ELSIF cout<=m THEN lcd_clk<='0';
	ELSE cout:=0;
	END IF;
END IF;
END PROCESS;

p3:PROCESS(lcd_clk,state)
VARIABLE cnt: INTEGER RANGE 0 TO 16 ;
BEGIN
--state<=set_function;
IF lcd_clk'EVENT AND lcd_clk='1' THEN
	IF(init='0') THEN
	state<=set_function;init<='1';
	END IF;
CASE state IS
WHEN set_function=>data<="00111000";rs<='0';state<=set_function2;
WHEN set_function2=>data<="00111000";rs<='0';state<=set_display;
WHEN set_display=>data<="00001100";rs<='0';state<=clear_display;
WHEN clear_display=>data<="00000001";rs<='0';state<=set_cursor;
WHEN set_cursor=>data<="00000110";rs<='0';state<=set_ddram1;
WHEN set_ddram1=>data<="10000000";rs<='0';state<=write_data1;
WHEN write_data1=>rs<='1';data<=ram1(cnt)(7 DOWNTO 0);cnt:=cnt+1;
IF cnt=16 THEN state<=set_ddram2; END IF;
WHEN set_ddram2=>rs<='0';data<="11000000";state<=write_data2;
WHEN write_data2=>rs<='1';data<=ram2(cnt)(7 DOWNTO 0);cnt:=cnt+1;
IF cnt=16 THEN state<=set_ddram1; END IF;
--IF cnt=16 THEN state<=set_function; END IF;
WHEN OTHERS=>NULL;
END CASE;
END IF;
END PROCESS;

--p4:PROCESS(clk,next_record)--read data from ram when user click next_record botton
--VARIABLE cnt:INTEGER RANGE 0 TO 2;
--BEGIN 
	--IF(clk'event and clk='1') THEN
	--cnt:=cnt+1;rdaddress<=rdaddress+1;
--	END IF;
	
	--IF(cnt=2) THEN
		
		
	--END IF;
--END PROCESS;

		


END fwm;